<?php
/**
 * 菜单Control类
 * @file 	MenuControl.class.php
 * @author 	itkele@gmail.com
 * @date 	2013-06-13
 * modify       
 */
class MenuControl extends Control{

    public function __construct(){
		parent::__construct();
		
		//子菜单
		$submenu = array(
			array(
				'alias' => 'menuList',
				'title' => '菜单列表',
				'url'	=> C('site_admin') . 'index.php?a=menu&m=menuList',
			),
			array(
				'alias' => 'addMenu',
				'title' => '添加菜单',
				'url'	=> C('site_admin') . 'index.php?a=menu&m=addMenu',
			),
		);
		
		$this->assign('mainMenu','menu');
		$this->assign('submenu',$submenu);
	}

	public function index(){
		$this->menuList();
	}
	
	/**
	 * 新加菜单
	 */
	public function addMenu($type='add'){
		$parent_menu = array(
			array('id' => 0, 'title' => '根菜单'),
			array('id' => 1, 'title' => '首页'),
		);
		$menu_type = array(
			array('id' => 1, 'name' => '首页'),
			array('id' => 2, 'name' => '分类'),
			array('id' => 3, 'name' => '组件'),
			array('id' => 4, 'name' => '外链'),
		);
		
		$component = array(
			array('id' => 1, 'name' => 'LinkCloud'),
			array('id' => 2, 'name' => 'Blog'),
		);
		
		$ordering = array_keys($parent_menu);
		$ordering = end($ordering) + 1;
		
		$this->assign('parent_menu',$parent_menu);
		$this->assign('menu_type',$menu_type);
		$this->assign('component',$component);
		$this->assign('ordering',$ordering);
		
		if($_POST['dosubmit'] == 1){
			$data = array(
				'id' => (int)$_POST['menu_id'],
				'menutype' => $_POST['menutype'],
				'title' => $_POST['title'],
				'alias' => $_POST['alias'],
				'link' => $_POST['link'],
				'type' => $_POST['type'],
				'parent_id' => (int)$_POST['parent_id'],
				'component_id' => (int)$_POST['component_id'],
				'ordering' => (int)$_POST['ordering'],
				'params' => $_POST['params'],
				'status' => (int)$_POST['status'],
			);
			
			if(empty($data['id'])){
				$result = M('Menu')->addMenu($data);
			}else{
				$result = M('Menu')->editMenu($data);
			}
			
			$url = C('site_admin').'index.php?c=menu&m=menuList';
			$msg = '操作'.($result ? '成功' : '失败').'...';
			//$msg = iconv('utf-8','gbk',$msg);
			redirect($url,3,$msg);
		}else{
			//展示表单
			$menu_id = (int)$_GET['menu_id'];
			if( !empty($menu_id) ){
				$menu = M('Menu')->getMenuById($menu_id);
			}
			
			if($type == 'edit'){
				$this->assign('subMenu','editCategory');
			}else{
				$this->assign('subMenu','addCategory');
			}
		}
		
		$this->assign('menu_id',$menu_id);
		$this->assign('menu',$menu);
		$this->assign('formToken',token());
		$this->display('add_menu');
	}
	
	//---------------------debug----------------------------
	
	/**
	 * 删除菜单
	 * @return json
	 */
	public function delMenu(){
		$id = (int)$_POST['id'];
		$result = M('Category')->delMenu($id);
		$info = '删除'.($result ?  '成功' : '失败');
		$status = (int)$result;
		ajaxReturn('',$info,$status);
	}
	
	/**
	 * 编辑菜单
	 */
	public function editMenu(){
		$this->addMenu('edit');
	}
	
	/**
	 * 分类列表
	 */
	public function menuList(){
		$menuList = M('Menu')->menuList();
		$this->assign('current_submenu','menuList');
		$this->assign('menuList',$menuList);
		
		$this->display('menu_list');
	}

}